package com.ruoyi.project.user.partner.controller;

import com.ruoyi.common.constant.SqlOrder;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.config.RuoYiConfig;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.user.host.domain.Host;
import com.ruoyi.project.user.partner.domain.Partner;
import com.ruoyi.project.user.partner.service.PartnerService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;

@Controller
@RequestMapping("/user/partner")
public class PartnerController extends BaseController{
    private String prefix = "user/partner";

    @Autowired
    private PartnerService partnerService;

    @RequiresPermissions("user:partner:view")
    @GetMapping("")
    public String showView(){
        return prefix +"/partner";
    }

    /**
     * 显示页面数据
     *
     * @return
     */
    @RequiresPermissions("user:partner:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo demo(Partner partner) {
        startPage();
        List<Partner> list = partnerService.showPartner(partner);
        return getDataTable(list);
    }

    /**
     * 添加合作伙伴
     * @return
     */
    @RequiresPermissions("user:partner:add")
    @GetMapping("/add")
    public String showAdd() {
        return prefix + "/add";
    }
    /**
     * 检查网址是否存在
     *
     * @param partnerNet
     * @return
     */
    @RequiresPermissions("user:partner:add")
    @PostMapping("/checkNet")
    @ResponseBody
    public Long checkNet(String partnerNet) {
        return partnerService.checkNet(partnerNet);
    }

    /**
     * 新增数据
     *
     * @return
     */
    @RequiresPermissions("user:partner:add")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult add(Partner partner,@RequestParam("file") MultipartFile file) throws IOException {
        // 上传文件路径
        String filePath = RuoYiConfig.getUploadPath();
        // 上传并返回新文件名称
        String fileName = FileUploadUtils.upload(filePath, file);
       partner.setPartnerLogo(fileName);

        return toAjax(partnerService.insert(partner));
    }

    /**
     * 多条删除
     * @param ids
     * @return
     */
    @RequiresPermissions("user:partner:remove")
    @RequestMapping("/remove")
    @ResponseBody
    public AjaxResult remove(Long[] ids) {
        return toAjax(partnerService.delete(ids));
    }

    @RequiresPermissions("user:partner:edit")
    @GetMapping("/edit/{partnerId}")
    public String showEdit(@PathVariable Long partnerId, Model model){
        model.addAttribute("partner",partnerService.showPartnerById(partnerId));
        return prefix+"/edit";
    }
    @RequiresPermissions("user:partner:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult edit(Partner partner,@RequestParam("file") MultipartFile file) throws IOException {
        // 上传文件路径
        String filePath = RuoYiConfig.getUploadPath();
        // 上传并返回新文件名称
        String fileName = FileUploadUtils.upload(filePath, file);
        partner.setPartnerLogo(fileName);
        return toAjax(partnerService.updatePartner(partner));
    }





}
